package com.intel.two.service.medical.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.bo.building.ResidentBo;
import com.intel.two.bo.medical.ArmariumBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.controller.medical.ArmariumContorller;
import com.intel.two.mapper.medical.ArmariumMapper;
import com.intel.two.pojo.building.Resident;
import com.intel.two.pojo.medical.Armarium;
import com.intel.two.service.medical.ArmariumService;
import com.intel.two.vo.medical.ArmariumVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author:xiaofeia
 * @DATE:2022/10/13
 */
@Service
public class ArmariumServiceImpl implements ArmariumService {

    @Autowired
    private ArmariumMapper armariumMapper;


    @Override
    public LayuiPageVo<Armarium> armariumPageData(Map<String, Object> params) {
        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<Armarium> armariums =  armariumMapper.armariumPageData(params);

        // mybatis 的分页数据结构
        PageInfo<Armarium> armariumPageInfo = new PageInfo<>(armariums);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        LayuiPageVo<Armarium> layuiPageVo = new LayuiPageVo<>();
        layuiPageVo.setCode(0);
        layuiPageVo.setMsg("学生分页列表数据");
        layuiPageVo.setCount(armariumPageInfo.getTotal());  // 总记录数
        layuiPageVo.setData(armariumPageInfo.getList());  // 分页列表数据


//        Map<String,Object> map = new HashMap<>();
//        map.put("code",0);
//        map.put("count",armariumPageInfo.getTotal());
//        map.put("data",armariumPageInfo.getList());
//        map.put("msg","fasd");
//        map.put("armariums",armariums);

        return layuiPageVo;
    }

    @Override
    public int addArmarium(Armarium armarium) {
        return armariumMapper.addArmarium(armarium);
    }

    @Override
    public int deleteArmariumById(Integer armarium_id) {
        return armariumMapper.deleteArmariumById(armarium_id);
    }

    @Override
    public int deletArmariumIds(String armarium_ids) {
        String[] _ids = armarium_ids.split(",");
        List<Integer> list = new ArrayList<>();
        for(int i=0;i < _ids.length; i++){
            list.add(Integer.parseInt(_ids[i]));
        }
        System.out.println(list);
        return armariumMapper.deletArmariumIds(list);
    }

    @Override
    public int updateArmarium(Armarium armarium) {
        return armariumMapper.updateArmarium(armarium);
    }

    @Override
    public List<ArmariumVo> amountBarData() {
        return armariumMapper. amountBarData();
    }

    @Override
    public List<ArmariumBo> armariumExcelData(Armarium armarium) {
        List<Armarium> armariums =  armariumMapper.armariumExcelData(armarium);
        //把po 转为bo
        List<ArmariumBo> collect = armariums.stream()
                .map((arm) -> {
                    ArmariumBo armariumBo = new ArmariumBo();
                    BeanUtils.copyProperties(arm, armariumBo);
                    return armariumBo;
                }).collect(Collectors.toList());

        return collect;

    }
}
